/**
	Provides simple cycling iteration for arrays, objects, and xml nodes.
	Items repeat forever

	@author Chandler McWilliams
	@version 2005-11-11

	@usage
	<code>
		var count = 0; // goes forever so limit this
		var looper = new CycleIterator(myData);
		while((item=looper.next()) !== false) {
			trace(looper.getCurrentKey() + ' = ' + item);
			if (++count>10) break;
		}
	</code>

*/
class timber.collection.CycleIterator extends timber.collection.Iterator
{
	public function CycleIterator(aCollection) {
		super(aCollection);
	}
	
	public function getNextIndex() {
		return (index+1) % length;
	}	

	public function getPrevIndex() {
		return ((index-1) < 0)?(length-1):(index-1);
	}
	
/**
	getOrderedCollection
	Returns the current collection in the current order of the cycle.
	In other words if the next() item is the 3rd thing, this will return a collection
	starting with 3 and ending with 2.

	@return (mixed) XML, Array, Iterable
**/

	public function getOrderedCollection() {
		var out;
		// switch for type
		switch(true) {
			case isIterable():
				if (!collection.concat || !collection.slice) {
					// TODO: Mkae this throw for AS2
					trace('Collection must implement slice() and concat() for CycleIterator.getOrderedCollection().');
					return false;
				}

			case isArray():
				out = collection.slice(getCurrentIndex());
				return out.concat(collection.slice(0, getCurrentIndex()));
			
			case isObject():
				return collection;

			case isXML():
				var item;
				out = collection;
				for (var i=0;i<getCurrentIndex();i++) {
					item = out.firstChild.childNodes[i].cloneNode(true);
					out.firstChild.appendChild(item);
					out.firstChild.childNodes[i].removeNode();
				}
				return out;
		}
	}
}